package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时保存套餐和菜品的关系
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //数据库需操作两个表

        //保存套餐的基本信息，操作setmeal表，执行insert操作
        this.save(setmealDto);
        //获取菜品与套餐关联对象集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //将菜品与套餐关联
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id查询套餐信息，(用于数据回显)
     * @param id
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //根据ID获取套餐对象
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //对象拷贝
        BeanUtils.copyProperties(setmeal, setmealDto);
        //创建条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件，查询setmeal_dish表中和前端传入对应的套餐id相同的数据
        queryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        //执行查询
        List<SetmealDish> dishes = setmealDishService.list(queryWrapper);
        //将菜品数据设置给setmealDto对象
        setmealDto.setSetmealDishes(dishes);
        //将结果对象返回
        return setmealDto;
    }

    /**
     * 修改套餐信息同时修改套餐菜品数据
     * @param setmealDto
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐基本信息
        this.updateById(setmealDto);

        //清理当前套餐包含菜品数据,setmeal_dish表的delete操作
        //创建条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //调用removeById()方法，清空dish数据
        setmealDishService.remove(queryWrapper);
        //添加当前提交过来的菜品数据，setmeal_dish表的insert操作
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
       setmealDishService.saveBatch(dishes);
    }


    /**
     * 删除/批量删除套餐数据同时删除套餐所关联菜品数据，删除之前判断套餐状态
     * @param ids
     */
    @Transactional
    @Override
    public void remove(List<Long> ids) {
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);

        int count = super.count(queryWrapper);
        if(count>0){
            //不能删除，抛出一个业务异常
            throw new CustomException("该套餐正在售卖，无法删除");
        }
        //如果可以删除，先删除setmeal基本数据
        super.removeByIds(ids);
        //再删除套餐关联的菜品数据
        //创建条件构造器
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        //添加条件
        qw.in(SetmealDish::getDishId,ids);
        setmealDishService.remove(qw);
    }

}
